package colakao;

import taliav2.Player;

/**
 *
 * @author Maciej Madaj & Piotr Drobinski
 *
 * Klasa pomocniczna
 * Posiada tylko metody statyczne
 * Odpowiada za sortowanie kart w 1 z 4 porzadkow
 *
 */
public class Util {
    /** The parameter sort_way determines what kind of sort is going to be made.
     * sort_way=1 (ascending, punish factor)
     * sort_way=2 (descending, punish factor)
     * sort_way=3 (ascending, rank factor)
     * sort_way=4 (descending, rank factor)
     */
    protected static void SortCards(int p_index, int sort_way, Player players[])
    {
        if(sort_way==1)
        {
            for(int i=0; i<players[p_index].tPlayer.size(); i++)
                for(int j=0; j<players[p_index].tPlayer.size()-1; j++)
                    sort_punish(p_index,j,0,players);
        }
        else if(sort_way==2)
        {
            for(int i=0; i<players[p_index].tPlayer.size(); i++)
                for(int j=0; j<players[p_index].tPlayer.size()-1; j++)
                    sort_punish(p_index,j,1,players);
        }
        else if(sort_way==3)
        {
            for(int i=0; i<players[p_index].tPlayer.size(); i++)
                for(int j=0; j<players[p_index].tPlayer.size()-1; j++)
                    sort_rank(p_index,j,0,players);
        }
        else if(sort_way==4)
        {
            for(int i=0; i<players[p_index].tPlayer.size(); i++)
                for(int j=0; j<players[p_index].tPlayer.size()-1; j++)
                    sort_rank(p_index,j,1,players);
        }
        for(int i=0; i<players[p_index].tPlayer.size(); i++)
            players[p_index].tPlayer.get(i).ind=i;
    }
    /** Sorting player cards according to punish of the Card and in specified order.*/
    private static void sort_punish(int p_index, int index, int order, Player players[])
    {
        int r1=SetPunishInt(p_index,index,players);
        int r2=SetPunishInt(p_index,index+1,players);
        if(r1<r2 && order==0)
            Swap(p_index,index,index+1,players);
        else if(r1>r2 && order==1)
            Swap(p_index,index,index+1,players);
    }
    /** Sorting player cards according to rank of the Card and in specified order.*/
    private static void sort_rank(int p_index, int index, int order, Player players[])
    {
        int r1=SetRankInt(p_index,index,players);
        int r2=SetRankInt(p_index,index+1,players);
        if(r1<r2 && order==0)
            Swap(p_index,index,index+1,players);
        else if(r1>r2 && order==1)
            Swap(p_index,index,index+1,players);
    }
    /** Swaping 2 card with each other*/
    private static void Swap(int p_index, int from, int to, Player players[]) 
    {
        Card swap = new Card();
        swap.setCard(players[p_index].getCard(from));
        players[p_index].setCard(players[p_index].getCard(to),from);
        players[p_index].setCard(swap,to);
    }
    /** Giving bigger values for specified cards according to Card rank. Order (descending):
     * Joker, Ace, King, Queen, Jack, 10... and so on.*/
    private static int SetRankInt(int p_index, int index,Player players[])
    {
        if(players[p_index].tPlayer.get(index).rank=='1')
            return 10;
        else if(players[p_index].tPlayer.get(index).rank=='J')
            return 11;
        else if(players[p_index].tPlayer.get(index).rank=='Q')
            return 12;
        else if(players[p_index].tPlayer.get(index).rank=='K')
            return 13;
        else if(players[p_index].tPlayer.get(index).rank=='A')
            return 14;
        else if(players[p_index].tPlayer.get(index).rank=='B')
            return 15;
        else
            return players[p_index].tPlayer.get(index).rank-48;
    }
    /** Giving bigger values for specified cards according to Card punish. Order (descending):
     * Joker, King (Spades & Hearts), 3, 2, 4, Ace, Jack, King, Queen, Jack, 10... and so on.*/
    private static int SetPunishInt(int p_index, int index,Player players[])    
    {
        if(players[p_index].tPlayer.get(index).rank=='B')
            return 19;
        else if(players[p_index].tPlayer.get(index).rank=='K' &&
                (players[p_index].tPlayer.get(index).color=='S' || players[p_index].tPlayer.get(index).color=='H'))
            return 18;
        else if(players[p_index].tPlayer.get(index).rank=='3')
            return 17;
        else if(players[p_index].tPlayer.get(index).rank=='2')
            return 16;
        else if(players[p_index].tPlayer.get(index).rank=='4')
            return 15;
        else if(players[p_index].tPlayer.get(index).rank=='A')
            return 14;
        else if(players[p_index].tPlayer.get(index).rank=='J')
            return 13;
        else if(players[p_index].tPlayer.get(index).rank=='K')
            return 12;
        else if(players[p_index].tPlayer.get(index).rank=='Q')
            return 11;
        else if(players[p_index].tPlayer.get(index).rank=='1')
            return 10;
        else
            return players[p_index].tPlayer.get(index).rank-48;
    }
}